package com.lolong.main;

import java.net.URL;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.util.Log;
import com.lolong.model.ParsedData;
import com.lolong.controller.WeatherMessageHandler;
import com.lolong.controller.WeatherHandler;

public class WeatherConnector {

	private String MY_DEBUG_TAG = "WeatherForcaster";
	public ParsedData parsedData;
	public WeatherMessageHandler weatherMessageHandler;
	
	WeatherConnector() {
	
		weatherMessageHandler = new WeatherMessageHandler();
			
		try {
			
             URL url = new URL("http://173.194.35.176/ig/api?weather=Makati");
        
             SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
             SAXParser saxParser = saxParserFactory.newSAXParser();
             XMLReader xmlReader = saxParser.getXMLReader();
             WeatherHandler weatherHandler = new WeatherHandler();
             
             xmlReader.setContentHandler(weatherHandler);
             xmlReader.parse(new InputSource(url.openStream()));

             this.parsedData = weatherHandler.getParsedData();
            
		} catch (Exception e) {
			
   	 		System.out.println("eto error" + e.getMessage());
   	 		Log.e(MY_DEBUG_TAG, "WeatherQueryError", e);
   	 		
	   	}
	}
	
	public String returnSuggestion() {
		
		weatherMessageHandler.generateClothingSuggestion(parsedData.getParsedForecast());
		
		return weatherMessageHandler.getClothingSuggestion();
		
	}
	
	
}